#include "NetworkProtocol.h"


NetworkProtocol::NetworkProtocol()
{
	NumDevice = 0;
	Num1042 = 0;
	Num1142 = 0;
	Num64 = 0;
	Num66 = 0;
	Flash_201 = 0;
	ResetFrame((CastFrame*)&VConfigFrame);
}

NetworkProtocol::~NetworkProtocol()
{
}

int NetworkProtocol::AddDevices(wxString Devices,int nDevice)
{
	int ret;
	NumDevice = nDevice;
	for (int i=0; i<NumDevice; i++)	//Max decices is 4
	{
		if (Devices.IsSameAs(wxT("EEG-1142")))
		{
			if (Num1142 <2)
			{
				VConfigFrame.Address[i] = Num1142+5;
				VConfigFrame.ChDevice[i] = 0x2A;
				Num1142++;
				ret = 1;
			}
			else
			{
				wxMessageBox (wxT("Attention please! To many EEG-1142 configured!"));
				ret = 0;
			}
		}	
		else if (Devices.IsSameAs(wxT("EEG-1042")))
		{
			if (Num1042 <2)
			{
				VConfigFrame.Address[i] = Num1042+7;
				VConfigFrame.ChDevice[i] = 0x2A;
				Num1042++;
				ret = 1;
			}
			else
			{
				wxMessageBox (wxT("Attention please! To many EEG-1042 configured!"));
				ret = 0;
			}
		}
		else if (Devices.IsSameAs(wxT("EEG-1164"))) 
		{
			if (Num64<2)
			{
				VConfigFrame.Address[i] = Num64+1;
				VConfigFrame.ChDevice[i] = 0x40;
				Num64++;
				ret = 1;
			}
			else
			{
				wxMessageBox (wxT("Attention please! To many EEG-1164 configured!"));
				ret = 0;
			}
		}
		else if (Devices.IsSameAs(wxT("EEG-1166")))
		{
			if (Num66 <4)
			{
				VConfigFrame.Address[i] = Num66+9;
				VConfigFrame.ChDevice[i] = 0x40;
				Num66++;
				ret = 1;
			}
			else
			{
				wxMessageBox (wxT("Attention please! To many EEG-1166 configured!"));
				ret = 0;
			}
		}
		else if (Devices.IsSameAs(wxT("FLASH-201")))
		{	
			if (Flash_201 == 0)
			{
				VConfigFrame.Address[i] = 14;
				VConfigFrame.ChDevice[i] = 0x02;
				Flash_201 = 1;
				ret = 1;
			}
			else
			{
				wxMessageBox (wxT("Attention please! To many Flash-201 configured!"));
				ret = 0;
			}
		}
	}
	return ret;

}

void NetworkProtocol::ResetFrame (CastFrame *Frame)
{
	for (int i=0; i<132; i++)
		Frame->bytes[i] = 0x00;
}

void NetworkProtocol::SetConfigFrame(int SampleRate)
{
	VConfigFrame.Header.String[0] = 'c';
	VConfigFrame.Header.String[1] = '3';
	VConfigFrame.Header.Byte3 = 1;	//Version
	switch(SampleRate)
	{
	case 1025:
		VConfigFrame.SampleRate[0]= 0x00;	//LowBit
		VConfigFrame.SampleRate[1]= 0x04; //HighBit
		break;
	case 512:
		VConfigFrame.SampleRate[0]= 0x00;	//LowBit
		VConfigFrame.SampleRate[1]= 0x02; //HighBit
		break;
	case 245:
		VConfigFrame.SampleRate[0]= 0x00;	//LowBit
		VConfigFrame.SampleRate[1]= 0x01; //HighBit
		break;
	case 124:
		VConfigFrame.SampleRate[0]= 0x80;	//LowBit
		VConfigFrame.SampleRate[1]= 0x00; //HighBit
		break;
	}


}

void NetworkProtocol::SetStartFrame(void)
{
	ResetFrame((CastFrame*)&VRequestFrame);
	VRequestFrame.Header.String[0] = 'c';
	VRequestFrame.Header.String[1] = '1';
	VRequestFrame.Header.Byte2= 0;
	VRequestFrame.Header.Byte3 = 1;
	for (int i=0; i<128; i++)
		VRequestFrame.ToFill[i] = 0;
}

void NetworkProtocol::SetStopFrame(void)
{
	ResetFrame((CastFrame*)&VRequestFrame);
	VRequestFrame.Header.String[0] = 'c';
	VRequestFrame.Header.String[1] = '2';
	VRequestFrame.Header.Byte2= 0;
	VRequestFrame.Header.Byte3 = 1;
	for (int i=0; i<128; i++)
		VRequestFrame.ToFill[i] = 0;

}


void NetworkProtocol::SetEEGMode(void)
{
	VControlFrame.Header.String[0] = 'c';
	VControlFrame.Header.String[1] = '4';
	VControlFrame.Header.Byte2 = 0x00;
	VControlFrame.Header.Byte3 = 0x01;
	int i;

	for (i=0; i<NumDevice; i++)
	{
		VControlFrame.Array[i*2] = 0x80;	//LOW Byte
		VControlFrame.Array[i*2+1] = 0x00;	//HIGH Byte
	}
	for (i=NumDevice*2; i<128;i++)
		VControlFrame.Array[i] = 0x00;


}

void NetworkProtocol::SetImpedanceMode(void)
{
	VControlFrame.Header.String[0] = 'c';
	VControlFrame.Header.String[1] = '4';
	VControlFrame.Header.Byte2 = 0x00;
	VControlFrame.Header.Byte3 = 0x01;
	int i;

	for (i=0; i<NumDevice; i++)
	{
		VControlFrame.Array[NumDevice*2+i] = 0x08;	//LOW Byte
		VControlFrame.Array[NumDevice*2+i+1] = 0x03;	//HIGH Byte
	}
	for (i=NumDevice*2; i<128;i++)
		VControlFrame.Array[i] = 0x00;

}

void NetworkProtocol::SetCalibrationMode(void)
{
	VControlFrame.Header.String[0] = 'c';
	VControlFrame.Header.String[1] = '4';
	VControlFrame.Header.Byte2 = 0x00;
	VControlFrame.Header.Byte3 = 0x01;
	int i;

	for (i=0; i<NumDevice; i++)
	{
		VControlFrame.Array[NumDevice*2+i] = 0x20;	//LOW Byte
		VControlFrame.Array[NumDevice*2+i+1] = 0x00;	//HIGH Byte
	}
	for (i=NumDevice*2; i<128;i++)
		VControlFrame.Array[i] = 0x00;

}